<template>
  <div class="video-player" ref="videoContainer">
    <video
      ref="video"
      class="video-element"
      @loadedmetadata="onLoadedMetadata"
      @timeupdate="onTimeUpdate"
      @ended="onEnded"
      @play="onPlay"
      @pause="onPause"
      @volumechange="onVolumeChange">
      <source :src="videoSrc" type="video/mp4" />
      您的浏览器不支持视频播放。
    </video>

    <!-- 视频控制栏 -->
    <div class="video-controls" :class="{ 'controls-hidden': controlsHidden }">
      <!-- 进度条 -->
      <div class="progress-container">
        <div class="progress-bar" @click="seekTo">
          <div class="progress-filled" :style="{ width: progress + '%' }"></div>
          <div class="progress-thumb" :style="{ left: progress + '%' }"></div>
        </div>
        <div class="time-display">{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</div>
      </div>

      <!-- 控制按钮 -->
      <div class="controls-main">
        <div class="controls-left">
          <button class="control-btn" @click="togglePlay">
            <span v-if="isPlaying">⏸️</span>
            <span v-else>▶️</span>
          </button>

          <button class="control-btn" @click="skip(-10)">⏪ 10s</button>

          <button class="control-btn" @click="skip(10)">⏩ 10s</button>

          <div class="volume-control">
            <button class="control-btn" @click="toggleMute">
              <span v-if="isMuted">🔇</span>
              <span v-else-if="volume < 0.5">🔉</span>
              <span v-else>🔊</span>
            </button>
            <input
              type="range"
              class="volume-slider"
              min="0"
              max="1"
              step="0.1"
              v-model="volume"
              @input="onVolumeInput" />
          </div>
        </div>

        <div class="controls-right">
          <button class="control-btn" @click="toggleFullscreen">
            <span v-if="isFullscreen">⏹️</span>
            <span v-else>⛶</span>
          </button>
        </div>
      </div>
    </div>

    <!-- 播放按钮覆盖层 -->
    <div v-if="!isPlaying && !controlsHidden" class="play-overlay" @click="togglePlay">
      <div class="play-button">▶️</div>
    </div>

    <!-- 加载指示器 -->
    <div v-if="isLoading" class="loading-indicator">
      <div class="spinner"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "VideoPlayer",
  props: {
    videoSrc: {
      type: String,
      required: true,
    },
    title: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      isPlaying: false,
      isMuted: false,
      isFullscreen: false,
      isLoading: true,
      controlsHidden: false,
      currentTime: 0,
      duration: 0,
      volume: 1,
      controlsTimeout: null,
    }
  },
  computed: {
    progress() {
      return this.duration > 0 ? (this.currentTime / this.duration) * 100 : 0
    },
  },
  mounted() {
    this.setupControlsAutoHide()
    this.setupKeyboardControls()
  },
  beforeUnmount() {
    this.clearControlsTimeout()
    document.removeEventListener("keydown", this.handleKeydown, { passive: true })
  },
  methods: {
    // 播放控制
    togglePlay() {
      if (this.isPlaying) {
        this.$refs.video.pause()
      } else {
        this.$refs.video.play()
      }
    },

    // 快进快退
    skip(seconds) {
      this.$refs.video.currentTime += seconds
    },

    // 音量控制
    toggleMute() {
      this.$refs.video.muted = !this.$refs.video.muted
      this.isMuted = this.$refs.video.muted
    },

    onVolumeInput() {
      this.$refs.video.volume = this.volume
      this.isMuted = this.volume === 0
    },

    // 进度控制
    seekTo(event) {
      const rect = event.currentTarget.getBoundingClientRect()
      const percent = (event.clientX - rect.left) / rect.width
      this.$refs.video.currentTime = percent * this.duration
    },

    // 全屏控制
    toggleFullscreen() {
      if (!this.isFullscreen) {
        if (this.$refs.videoContainer.requestFullscreen) {
          this.$refs.videoContainer.requestFullscreen()
        } else if (this.$refs.videoContainer.webkitRequestFullscreen) {
          this.$refs.videoContainer.webkitRequestFullscreen()
        }
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen()
        }
      }
    },

    // 时间格式化
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60)
      const secs = Math.floor(seconds % 60)
      return `${mins}:${secs.toString().padStart(2, "0")}`
    },

    // 事件处理
    onLoadedMetadata() {
      this.duration = this.$refs.video.duration
      this.isLoading = false
    },

    onTimeUpdate() {
      this.currentTime = this.$refs.video.currentTime
    },

    onPlay() {
      this.isPlaying = true
    },

    onPause() {
      this.isPlaying = false
    },

    onEnded() {
      this.isPlaying = false
    },

    onVolumeChange() {
      this.volume = this.$refs.video.volume
      this.isMuted = this.$refs.video.muted
    },

    // 控制栏自动隐藏
    setupControlsAutoHide() {
      this.$refs.videoContainer.addEventListener(
        "mousemove",
        () => {
          this.controlsHidden = false
          this.clearControlsTimeout()
          this.controlsTimeout = setTimeout(() => {
            if (this.isPlaying) {
              this.controlsHidden = true
            }
          }, 3000)
        },
        { passive: true }
      )
    },

    clearControlsTimeout() {
      if (this.controlsTimeout) {
        clearTimeout(this.controlsTimeout)
        this.controlsTimeout = null
      }
    },

    // 键盘控制
    setupKeyboardControls() {
      document.addEventListener("keydown", this.handleKeydown, { passive: true })
    },

    handleKeydown(event) {
      switch (event.code) {
        case "Space":
          event.preventDefault()
          this.togglePlay()
          break
        case "ArrowLeft":
          event.preventDefault()
          this.skip(-5)
          break
        case "ArrowRight":
          event.preventDefault()
          this.skip(5)
          break
        case "ArrowUp":
          event.preventDefault()
          this.volume = Math.min(1, this.volume + 0.1)
          break
        case "ArrowDown":
          event.preventDefault()
          this.volume = Math.max(0, this.volume - 0.1)
          break
        case "KeyM":
          event.preventDefault()
          this.toggleMute()
          break
        case "KeyF":
          event.preventDefault()
          this.toggleFullscreen()
          break
      }
    },
  },
}
</script>

<style scoped>
.video-player {
  position: relative;
  width: 100%;
  max-width: 100%;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: 16/9;
}

.video-element {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.video-controls {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
  padding: 20px;
  transition: opacity 0.3s ease;
}

.controls-hidden {
  opacity: 0;
  pointer-events: none;
}

.progress-container {
  margin-bottom: 15px;
}

.progress-bar {
  position: relative;
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  cursor: pointer;
  margin-bottom: 8px;
}

.progress-filled {
  height: 100%;
  background: var(--primary);
  border-radius: 2px;
  transition: width 0.1s ease;
}

.progress-thumb {
  position: absolute;
  top: 50%;
  width: 12px;
  height: 12px;
  background: var(--primary);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: left 0.1s ease;
}

.time-display {
  color: white;
  font-size: 0.85rem;
  text-align: right;
}

.controls-main {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.controls-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.controls-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.control-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s ease;
  backdrop-filter: blur(10px);
}

.control-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.volume-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.volume-slider {
  width: 60px;
  height: 4px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

.volume-slider::-webkit-slider-thumb {
  appearance: none;
  width: 12px;
  height: 12px;
  background: var(--primary);
  border-radius: 50%;
  cursor: pointer;
}

.play-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.3);
  cursor: pointer;
}

.play-button {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  backdrop-filter: blur(10px);
  transition: all 0.2s ease;
}

.play-button:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.loading-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top: 4px solid var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 全屏样式 */
.video-player:fullscreen {
  width: 100vw;
  height: 100vh;
}

.video-player:-webkit-full-screen {
  width: 100vw;
  height: 100vh;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .video-controls {
    padding: 15px;
  }

  .control-btn {
    padding: 6px 10px;
    font-size: 0.8rem;
  }

  .volume-slider {
    width: 40px;
  }

  .play-button {
    width: 60px;
    height: 60px;
    font-size: 1.5rem;
  }
}
</style>
